﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KaiXin.BLL.Interface;
using KaiXin.DTO.Tycoon;
using KaiXin.DAL.XML;

namespace KaiXin.BLL.Tycoon
{
    public class Order:Robot,IOrder
    {
        public Order(string e, string p) : base(e, p) { }

        #region field
        private Persistence<Orders> _dal = new OrdersDAL();
        #endregion

        #region IOrder 成员

        public List<Orders> GetMyOrders()
        {
            return _dal.GetAllItem().Where(o => o.Email == Email).ToList();
        }

        public void CreateDefaultOrders(Func<Goods, bool> func)
        {
            IKXTycoon kx = new KXTycoon();
            foreach (var goods in kx.GetGoods())
            {
                if (func(goods))
                {
                    Orders dto = new Orders();
                    dto.Email = Email;
                    dto.Password = Password;
                    dto.GoodsID = goods.ID;
                    dto.Price = goods.MinPrice;
                    dto.IsSell = false;
                    SetOrder(dto);//买入委托
                    //卖出20%
                    dto.Price = (long)((goods.MinPrice * 1.2) > goods.MaxPrice ? goods.MaxPrice : (goods.MinPrice * 1.2));
                    dto.IsSell = true;
                    SetOrder(dto);//卖出委托
                }
            }
        }

        public void SetOrder(Orders order)
        {
            if (_dal.IsExist(order))
            {
                _dal.FixItem(order);
            }
            else
            {
                _dal.AddItem(order);
            }
            _dal.Save();
        }

        public void DelOrder(Orders order)
        {
            _dal.DeleteItem(order);
            _dal.Save();
        }

        #endregion
    }
}
